﻿
Partial Class WebForm_ManageVolunteer_GroupControl
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Event OK_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        RaiseEvent OK_Click(Me, New EventArgs)

    End Sub
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1
        Me.BindGV()
        Me.mpSearch.Show()
    End Sub


    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim GroupID As String = CType(gv.Rows(e.RowIndex).FindControl("lblgvGroupID"), Label).Text.Trim
        Dim gvGroupName As String = CType(gv.Rows(e.RowIndex).FindControl("txtgvGroupName"), TextBox).Text.Trim
        Dim bblUpdate As New BLL_GroupCotrol
        If gvGroupName.Trim() = "" Then
            MyObj.ShowMessage("請輸入小組名稱，此欄位不可空白!")
        ElseIf bblUpdate.GroupNameIsExist(Me.VOrganizationID, gvGroupName) = False OrElse gvGroupName = bblUpdate.GetOriGroupName(Me.VOrganizationID, GroupID) Then


            bblUpdate.UpdateData(gvGroupName, Me.VOrganizationID, GroupID)
            e.Cancel = True
            Me.gv.EditIndex = -1
        Else
            MyObj.ShowMessage(String.Format("您輸入的小組名稱：{0}已存在，請重新輸入！", gvGroupName))
            e.Cancel = True

        End If

        Me.BindGV()
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gv.RowUpdated
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub
    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim GroupID As String = CType(gv.Rows(e.RowIndex).FindControl("lblgvGroupID"), Label).Text.Trim
        Dim bblDelete As New BLL_GroupCotrol

        bblDelete.DeleteData(Me.VOrganizationID, GroupID)
        Me.gv.EditIndex = -1
        Me.BindGV()
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gv.RowDeleted
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub
    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing

        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
        Me.mpSearch.Show()
    End Sub
    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
    
        Dim bllInsert As New BLL_GroupCotrol
        If Me.txtGroupName.Text.Trim() = "" Then
            MyObj.ShowMessage("請輸入小組名稱，此欄位不可空白!")
        ElseIf bllInsert.GroupNameIsExist(Me.VOrganizationID, Me.GroupName) = False Then
            bllInsert.InsertData(Me.GroupName, Me.VOrganizationID)
            Me.BindGV()
            Me.txtGroupName.Text = ""
        Else
            MyObj.ShowMessage(String.Format("您輸入的小組名稱：{0}已存在，請重新輸入！", Me.GroupName))
            Me.txtGroupName.Text = ""
            Me.txtGroupName.Focus()
        End If

        Me.mpSearch.Show()
    End Sub
    Protected Sub BindGV()
        Dim bblSelect As New BLL_GroupCotrol

        gv.DataSource = bblSelect.SelectData(Me.VOrganizationID)
        gv.DataBind()
    End Sub
    Public Sub InitAndShow()
        Me.txtGroupName.Text = ""
        Me.BindGV()
        Me.mpSearch.Show()
    End Sub
    Public Property VOrganizationID() As Integer
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As Integer)
            ViewState("VOrganizationID") = value
        End Set
    End Property

    Public ReadOnly Property GroupName() As String
        Get
            Return Me.txtGroupName.Text.Trim()
        End Get
    End Property


End Class
